package com.wgt;

/**
 * Created by wgt on 2017/5/31.
 */


import org.omg.CosNaming.NamingContextPackage.NotFound;

import java.io.*;

/**
 * copy文件，修改文件名
 */
public class Jad2Java {


    public static void main(String[] args) throws IOException {
        new Jad2Java().selectDirList("F:\\File\\2017");
    }


    public void selectDirList(String path) throws IOException {
        File dir = new File(path);
        if (!dir.exists() && !dir.isDirectory()) {
            throw new FileNotFoundException("dir " + path + " not found !");
        }

        File[] files = dir.listFiles(new FileNameFilter());
        String destDirPath = "F:\\File\\2017\\222\\";
        for (File file : files) {
            copyFile(file,destDirPath);
        }
    }


    public void copyFile(File file, String targetDirPath) throws IOException {
        if (file == null || targetDirPath.isEmpty()) {
            throw new NullPointerException("parameters is null！");
        }
        File destFile =null;
        BufferedReader bfreader = null;
        BufferedWriter bfwrite = null;
        try {
            String destFileName = targetDirPath + file.getName().replaceAll("\\.abc$", ".java");
            destFile = new File(destFileName);
            if (!destFile.exists()) {
                destFile.createNewFile();
            }
            bfreader = new BufferedReader(new FileReader(file));
            bfwrite= new BufferedWriter(new FileWriter(destFileName));
            String content = null;
            while((content=bfreader.readLine())!=null){
                bfwrite.write(content+"\r\n");
                bfwrite.flush();
            }
        } catch (IOException e1) {
            if (destFile!=null&&destFile.exists()){
                destFile.delete();
            }
            e1.printStackTrace();
        }finally {
            if (bfreader!=null){bfreader.close();}
            if (bfwrite!=null){bfwrite.close();}
        }
    }
}

class FileNameFilter implements FilenameFilter{

    @Override
    public boolean accept(File dir, String name) {
        return name.endsWith(".abc");
    }
}